package com.taichuan.selfcheck.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.taichuan.selfcheck.R;


/**
 * 消息提示dialog
 * Created by yylqn on 2019.12.27
 */
public class MessageDialog extends Dialog {

    private TextView tips;//弹框文本内容
    private TextView txt_title;//提示
    private Button normal;//正常
    private Button error;//异常
    private Context mContext;


    private NormalOnClickListener normalOnClickListener;//正常按钮被点击了的监听器
    private ErrorOnClickListener errorOnClickListener;//异常按钮被点击了的监听器

    public MessageDialog(@NonNull Context context) {
        super(context);
        this.mContext=context;
    }

    public MessageDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.dialog_message);

        setCancelable(true);

        getWindow().setBackgroundDrawableResource(android.R.color.transparent);//去掉白色背景(设置背景透明)

        //初始化界面控件
        initView();

        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        normal = findViewById(R.id.btn_result_normal);
        error = findViewById(R.id.btn_result_error);
        txt_title = findViewById(R.id.txt_title);
        tips = findViewById(R.id.txt_result_tip);
        txt_title.setText("提示");
        tips.setText("本地已存有视频，是否继续下载? (1 取消 3 确定)");
        normal.setText(mContext.getString(R.string.sure));
        error.setText(mContext.getString(R.string.cancel));
    }

    /**
     * 初始化界面的正常按钮和异常按钮
     */
    private void initEvent() {
        //正常按钮被点击后，向外界提供监听
        normal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (normalOnClickListener != null) {
                    normalOnClickListener.onNormalClick();
                }
            }
        });
        //异常按钮被点击后，向外界提供监听
        error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (errorOnClickListener != null) {
                    errorOnClickListener.onErrorClick();
                }
            }
        });
    }

    public void setNormalOnClickListener(NormalOnClickListener normalOnclickListener){
        this.normalOnClickListener = normalOnclickListener;
    }

    public void setErrorOnClickListener(ErrorOnClickListener errorOnClickListener){
        this.errorOnClickListener = errorOnClickListener;
    }

    /**
     * 兼容按键机型
     * @param event
     * @return
     */
    @Override
    public boolean dispatchKeyEvent(@NonNull KeyEvent event) {
        switch (event.getKeyCode()){
            case KeyEvent.KEYCODE_1:
                error.performClick();
                break;
            case KeyEvent.KEYCODE_3:
                normal.performClick();
                break;
        }
        return false;
    }

    /**
     * 测试结果正常按钮 回调
     */
    public interface NormalOnClickListener{
        void onNormalClick();
    }

    /**
     * 测试结果异常按钮 回调
     */
    public interface ErrorOnClickListener{
        void onErrorClick();
    }
}
